/***************************************************************************
                          DataMgr.cpp  -  description
                             -------------------
    begin                : October 29 2009
    copyright            : Copyright 2009, ACKERMANN Thibaut
    email                : tackerma@edu.univ-fcomte.fr
    website              : http://code.google.com/p/nover-project/
    license              : GNU GPL v3
 ***************************************************************************/
/* *************************************************************************
 *  This file is part of NOver-Project.
 *  
 *  NOver-Project is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  NOver-Project is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with NOver-Project.  If not, see <http://www.gnu.org/licenses/>.
 * *************************************************************************/


#include "DataMgr.hpp"


DataMgr::DataMgr()
{
}


DataMgr::~DataMgr()
{
	while(Countries.size())
	{
		delete Countries.back();
		Countries.pop_back();
	}
	
	while(Continents.size())
	{
		delete Continents.back();
		Continents.pop_back();
	}
}


void DataMgr::load()
{
	loadContinents();
}


void DataMgr::loadContinents() throw (MyException)
{
	TiXmlDocument xmldoc;
	
	if(!xmldoc.LoadFile(Path(MediaPath).a("Continents").a("continents").genc()))
	{
		throw MyException("Impossible de charger/parser le fichier continents", __FILE__, __LINE__);
	}
	
	TiXmlHandle docHandle(&xmldoc);
	
	int i = 0;
	TiXmlElement* child = 0;
	
	while((child = docHandle.Child("continent", i).ToElement()))
	{
		// <continent>fichier</continent>

		std::string fichier = std::string(child->GetText());
		
		Continent* c = loadContinent(fichier);
		
		if(c)
			Continents.push_back(c);
		
		i++;
	}
	
	cout << "DataMgr : " << Continents.size() << " continent(s) trouvé(s)" << endl;
}


Continent* DataMgr::loadContinent(string fichier)
{
	TiXmlDocument xmldoc;
	
	if(!xmldoc.LoadFile(Path(MediaPath).a("Continents").a(fichier+".continent").genc()))
	{
		return 0;
	}
	
	TiXmlHandle docHandle(&xmldoc);
	
	TiXmlElement* e_name = docHandle.FirstChild("name").ToElement();
	
	if(!e_name)
		return 0;
	
	TiXmlElement* e_map = docHandle.FirstChild("map").ToElement();
	
	if(!e_map)
		return 0;
	
	Continent* c = new Continent();
	
	c->name = std::string(e_name->GetText());
	c->map = std::string(e_map->GetText());
	
	
	int i = 0;
	int count = 0;
	TiXmlElement* child = 0;
	
	while((child = docHandle.Child("country", i).ToElement()))
	{
		//<rectpays x1="" y1="" x2="" y2="">fichier</rectpays>
		int x1 = 0;
		int x2 = 0;
		int y1 = 0;
		int y2 = 0;
		child->QueryIntAttribute("x1", &x1);
		child->QueryIntAttribute("x2", &x2);
		child->QueryIntAttribute("y1", &y1);
		child->QueryIntAttribute("y2", &y2);
		
		Country* ctry = loadCountry(child->GetText());
		
		if(ctry)
		{
			ctry->X1 = x1;
			ctry->Y1 = y1;
			ctry->X2 = x2;
			ctry->Y2 = y2;
			
			// on l'enregistre dans la liste de tout les pays
			Countries.push_back(ctry);
			
			// et aussi dans la liste des pays du continent en question.
			// cela crée une redondance mais plus rapide et simple pour une recherche d'un pays.
			c->Countries.push_back(ctry);
			
			count++;
		}
		
		i++;
	}
	
	cout << "DataMgr : " << count << " pays trouvé(s) sur le continent " << c->name << endl;
	
	return c;
}


Country* DataMgr::loadCountry(string fichier)
{
	TiXmlDocument xmldoc;
	
	if(!xmldoc.LoadFile(Path(MediaPath).a("Continents").a("Countries").a(fichier+".country").genc()))
	{
		return 0;
	}
	
	TiXmlHandle docHandle(&xmldoc);
	
	TiXmlElement* e_name = docHandle.FirstChild("name").ToElement();
	if(!e_name)
		return 0;
	
	TiXmlElement* e_map = docHandle.FirstChild("map").ToElement();
	if(!e_map)
		return 0;
	
	Country* c = new Country();
	
	c->name = std::string(e_name->GetText());
	c->map = std::string(e_map->GetText());
	
	return c;
}






